<template>
  <div class="app-container">
    
    <el-collapse v-model="activeNames" @change="handleChange">
      <el-collapse-item title="还款计划" name="1">
        <el-row style="margin-top:20px;font-size:14px;color:#666">
          <el-col :span="2">客户：{{ order.real_name }}</el-col>
          <el-col :span="3">医院：{{ order.sort_name }}</el-col>
          <el-col :span="4">项目：{{ project }}</el-col>
          <el-col :span="2">项目本金：{{ order.total_money }}</el-col>
      
          <el-col :span="3" v-if="order.investor_id==1">首打款：{{ order.first_pay }}</el-col>
          <!-- <el-col :span="2" v-if="order.investor_id==1">回款周期：{{ order.hkzq }}</el-col> -->
          <el-col :span="3" v-if="order.investor_id==1">结算账户：{{ order.settle_account }}</el-col>
          <el-col :span="2" v-if="order.investor_id==1">已结算：{{ order.settle_money }}</el-col>
          <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" style="margin-top:8px" @click="excelout">导出</el-button>
        </el-row>

        <el-table :key="tableKey" v-loading="listLoading" :data="list" :row-class-name="tableRowClassName" border fit highlight-current-row style="width: 100%;margin-top:20px">

          <el-table-column label="还款期数" align="center" width="50px">
            <template slot-scope="{row}">
              <span>{{ row.qs }}</span>
            </template>
          </el-table-column>
          <el-table-column label="应还时间" align="center" width="100px">
            <template slot-scope="{row}">
              <span>{{ row.time_date }}</span>
            </template>
          </el-table-column>

          <!-- <el-table-column label="实际还款时间" align="center" width="110px">
              <template slot-scope="{row}">
                <span>{{ row.sh_time }}</span>
              </template>
            </el-table-column> -->
          <el-table-column label="还款状态" align="center" width="80px">
            <template slot-scope="{row}">
              <span>{{ row.status_name }}</span>
            </template>
          </el-table-column>
          <el-table-column label="还款金额" align="center" width="80px">
            <template slot-scope="{row}">
              <span>{{ (parseFloat(row.total_money)+parseFloat(row.server)).toFixed(2) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="本金" align="center" width="80px">
            <template slot-scope="{row}">
              <span>{{ row.money }}</span>
            </template>
          </el-table-column>
          <el-table-column label="利息" align="center" width="110px">
            <template slot-scope="{row}">
              <div class="input-box">
                <el-input v-model="row.money_rate" size="small" prop="money_rate" :disabled="true" />
                <!-- :disabled="row.is_paid==1?true:false"  -->
              </div>
            </template>
          </el-table-column>
          <el-table-column label="服务费" align="center" width="110px">
            <template slot-scope="{row}">
              <div class="input-box">
                <el-input v-model="row.server" size="small" prop="money_rate" :disabled="true" />
              </div>
            </template>
          </el-table-column>
          <el-table-column label="滞纳金" align="center" width="80px">
            <!-- <template slot-scope="{row}">
              <div class="input-box">
                <el-input v-model="row.late_fee" size="small" prop="late_fee" :disabled="row.is_paid==1?true:false" />
              </div>
            </template> -->
            <template slot-scope="{row}">
              <span>{{ row.late_fee }}</span>
            </template>
          </el-table-column>
          <el-table-column label="实际还款额" align="center" width="110px">
            <template slot-scope="{row}">
              <div class="input-box">
                <el-input v-model="row.paid_money" size="small" prop="paid_money" :disabled="row.is_paid==1?true:false" />
              </div>
            </template>
          </el-table-column>
          <el-table-column label="实际还款日期" align="center" width="130px">
            <template slot-scope="{row}">
              <div v-if="row.is_paid!=1 && row.paid_money!=0" class="block">
                <el-date-picker
                  v-model="value2"
                  style="width:140px"
                  format="yyyy-MM-dd"
                  type="datetime"
                  placeholder="选择日期"
                />
              </div>
              <span v-if="row.is_paid==1">{{ row.paid_date }}</span>
            </template>
          </el-table-column>
          <el-table-column label="还款方式" align="center" width="130px">
            <template slot-scope="{row}">
                <el-select v-model="row.payway" placeholder="请选择" :disabled="row.is_paid==1?true:false">
                <el-option
                  v-for="item in payway"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
                </el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="是否已还" align="center" width="100px">
            <template slot-scope="{row}">
              <el-select v-model="row.is_paid_bf" placeholder="请选择" :disabled="row.is_paid==1?true:false">
                <el-option
                  v-for="item in paid_options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="本金回款" align="center" width="90px">
            <template slot-scope="{row}">
              <span>{{ row.benjin }}</span>
            </template>
          </el-table-column>
          <!-- <el-table-column label="是否结算" align="center" width="50px">
            <template slot-scope="{row}">
              <span>{{ row.settle_name }}</span>
            </template>
          </el-table-column> -->
          <el-table-column label="是否结算" align="center" width="80px" v-if="order.investor_id==1">
            <template slot-scope="{row}">
              <el-select v-model="row.is_settle" placeholder="请选择">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="结算金额" align="center" width="110px" v-if="order.investor_id==1">
            <template slot-scope="{row}">
              <div class="input-box">
                <el-input v-model="row.settle_money" size="small" prop="settle_money"  />
              </div>
            </template>
          </el-table-column>
          <el-table-column label="结算时间" align="center" width="150px" v-if="order.investor_id==1">
            <template slot-scope="{row}">
              <div v-if="row.is_paid!=0&&row.settle_day==null" class="block">
                <el-date-picker
                  v-model="value1"
                  style="width:140px"
                  format="yyyy-MM-dd"
                  type="datetime"
                  placeholder="选择日期"
                />
              </div>
              <div v-if="row.is_paid!=0&&row.settle_day!=null" class="block">
                <el-date-picker
                  v-model="row.settle_day"
                  style="width:140px"
                  format="yyyy-MM-dd"
                  type="datetime"
                  placeholder="选择日期"
                />
              </div>
              <!-- <span v-if="row.settle_day!=''">{{ row.settle_day }}</span> -->
            </template>
          </el-table-column>

          <el-table-column label="操作" align="center" width="100" class-name="small-padding fixed-width">
            <template slot-scope="{row,$index}">
              <el-button  type="primary" size="mini" @click="onSubmit(row)">
                保存
              </el-button>
              <!-- <el-button v-if="row.can_bond==1&&row.is_paid==1" type="error" size="mini" @click="toBond(row)">
                转为保证金
              </el-button> -->
            </template>
          </el-table-column>
        </el-table>
      </el-collapse-item>
      <el-collapse-item title="云贷还款计划" name="2" v-if="order.investor_id==2">
        <el-row style="margin-top:20px;font-size:14px;color:#666">
          <el-col :span="3">客户：{{ haier_info.custName }}</el-col>
          <el-col :span="4">贷款发放金额：{{ haier_info.loanAmt }}</el-col>
          <el-col :span="4" >当前应还总金额：{{ haier_info.totalamt }}</el-col>
          <el-col :span="3" >预收款余额：{{ haier_info.prePaidFeeBal }}</el-col>
          <el-col :span="4" >本期应还金额：{{ haier_info.currTermAmt }}</el-col>
          <el-col :span="3">逾期金额：{{ haier_info.overDueAmt }}</el-col>
          <el-col :span="3" v-if="haier_info.payoffflag=='10'" style="color: #3FA5E8;">结清状态：已结清</el-col>
          <el-col :span="3" v-if="haier_info.payoffflag=='00'" >结清状态：未结清</el-col>
        </el-row>

        <el-table :key="tableKey" v-loading="listLoading2" :data="haier_plan_list" :row-class-name="tableRowClassNameHaier"  border fit highlight-current-row style="width: 100%;margin-top:20px">

          <el-table-column label="期次" align="center" >
            <template slot-scope="{row}">
              <span>{{ row.term }}</span>
            </template>
          </el-table-column>
          <el-table-column label="应还日期" align="center" >
            <template slot-scope="{row}">
              <span>{{ row.paidDate }}</span>
            </template>
          </el-table-column>
          <el-table-column label="应还本金" align="center" >
            <template slot-scope="{row}">
              <span>{{ row.shouldPrin }}</span>
            </template>
          </el-table-column>
          <el-table-column label="应还利息" align="center" >
            <template slot-scope="{row}">
              <span>{{ row.shouldInter }}</span>
            </template>
          </el-table-column>
          <el-table-column label="应还本金罚息" align="center" >
            <template slot-scope="{row}">
              <span>{{ row.shouldPrinPenalty }}</span>
            </template>
          </el-table-column>
          <el-table-column label="应还利息罚息" align="center" >
            <template slot-scope="{row}">
              <span>{{ row.shouldInterPenalty }}</span>
            </template>
          </el-table-column>
          <el-table-column label="应还总金额" align="center" >
            <template slot-scope="{row}">
              <span>{{ row.samt }}</span>
            </template>
          </el-table-column>
          <el-table-column label="实还本金" align="center" >
            <template slot-scope="{row}">
              <span>{{ row.realPrin }}</span>
            </template>
          </el-table-column>
          <el-table-column label="实还利息" align="center" >
            <template slot-scope="{row}">
              <span>{{ row.realInter }}</span>
            </template>
          </el-table-column>
          <el-table-column label="实还总金额" align="center" >
            <template slot-scope="{row}">
              <span>{{ row.ramt }}</span>
            </template>
          </el-table-column>
          <el-table-column label="还款状态" align="center" >
            <template slot-scope="{row}">
              <span v-if="row.paidFlag=='00'">未还款</span>
              <span v-if="row.paidFlag=='10'">部分还款</span>
              <span v-if="row.paidFlag=='20'">正常还款</span>
              <span v-if="row.paidFlag=='30'">提前还款</span>
              <span v-if="row.paidFlag=='40'">逾期还款</span>
              <span v-if="row.paidFlag=='50'">核销收回</span>
            </template>
          </el-table-column>
          <el-table-column label="还款渠道" align="center" >
            <template slot-scope="{row}">
              <span>{{ row.channel }}</span>
            </template>
          </el-table-column>
          <el-table-column label="还款日期" align="center" >
            <template slot-scope="{row}">
              <span  v-if="row.paidFlag=='20'">{{ row.paidDate }}</span>
            </template>
          </el-table-column>
         
        </el-table>
      </el-collapse-item>
      <el-collapse-item title="通融还款计划" name="3" v-if="order.investor_id==3">
        <!-- <el-row style="margin-top:20px;font-size:14px;color:#666">
          <el-col :span="3">客户：{{ haier_info.custName }}</el-col>
          <el-col :span="4">贷款发放金额：{{ haier_info.loanAmt }}</el-col>
          <el-col :span="4" >当前应还总金额：{{ haier_info.totalamt }}</el-col>
          <el-col :span="3" >预收款余额：{{ haier_info.prePaidFeeBal }}</el-col>
          <el-col :span="4" >本期应还金额：{{ haier_info.currTermAmt }}</el-col>
          <el-col :span="3">逾期金额：{{ haier_info.overDueAmt }}</el-col>
          <el-col :span="3" v-if="haier_info.payoffflag=='10'" style="color: #3FA5E8;">结清状态：已结清</el-col>
          <el-col :span="3" v-if="haier_info.payoffflag=='00'" >结清状态：未结清</el-col>
        </el-row> -->

        <el-table :key="tableKey" v-loading="listLoading2" :data="tongrong_plan_list" :row-class-name="tableRowClassNameHaier"  border fit highlight-current-row style="width: 100%;margin-top:20px">

          <el-table-column label="期次" align="center" >
            <template slot-scope="{row}">
              <span>{{ row.num }}</span>
            </template>
          </el-table-column>
          <el-table-column label="应还日期" align="center" >
            <template slot-scope="{row}">
              <span>{{ row.date }}</span>
            </template>
          </el-table-column>
          <el-table-column label="应还本金" align="center" >
            <template slot-scope="{row}">
              <span>{{ row.monthlyBenjin }}</span>
            </template>
          </el-table-column>
          <el-table-column label="应还利息" align="center" >
            <template slot-scope="{row}">
              <span>{{ row.monthlyRate }}</span>
            </template>
          </el-table-column>
          
          <el-table-column label="应还总金额" align="center" >
            <template slot-scope="{row}">
              <span>{{ row.monthlyPayment }}</span>
            </template>
          </el-table-column>
         
        </el-table>
      </el-collapse-item>
    </el-collapse>
  </div>
</template>

<script>
import {
  paymentList, paymentEdit, toBond,getPayWay,haierPaymentList,tongRongPaymentList,exceloutPlan
} from '@/api/finance'
import { formatDate } from '@/utils/formatDate.js'
import { merchantList } from '@/api/merchant'
import { fetchList } from '@/api/sale'
import waves from '@/directive/waves' // waves directive
import {
  parseTime
} from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
export default {
  name: 'ComplexTable',
  components: {
    Pagination
  },
  directives: {
    waves
  },
  filters: {},
  data() {
    return {
      tongrong_plan_list:[],
      haier_plan_list:[],
      haier_info:[],
      listLoading2:false,
      activeNames: ['1'],
      tableKey: 0,
      tableRecord: 1,
      list: null,
      payway:[],
      order: [],
      options: [{
          value: 1,
          label: '是'
        }, {
          value: 0,
          label: '否'
        }],
      paid_options: [{
        value: 1,
        label: '是'
      }, {
        value: 0,
        label: '否'
      }, {
        value: 2,
        label: '部分'
      }],
      project: '',
      record_list: null,
      listRecordLoading: true,
      recordTotal: 0,
      total: 0,
      listLoading: true,
      query: {
        order_id: ''
      },
      pickerOptions: {
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            picker.$emit('pick', new Date())
          }
        }, {
          text: '昨天',
          onClick(picker) {
            const date = new Date()
            date.setTime(date.getTime() - 3600 * 1000 * 24)
            picker.$emit('pick', date)
          }
        }, {
          text: '一周前',
          onClick(picker) {
            const date = new Date()
            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', date)
          }
        }]
      },
      value1: '',
      value2: '',
      value3: ''
    }
  },
  created() {
    this.getList();
    this.getPayWay();
  },
  methods: {
   
    getHaierList() {
      this.query.order_id = this.$route.params.order_id
      haierPaymentList(this.query).then(response => {
        if(response.data.code==0){
          this.haier_info = response.data.result
          this.haier_plan_list = response.data.result.billQueryResultItems
        }
        this.listLoading2 = false
      })
    },
    getTongRongList(){
      this.query.order_id = this.$route.params.order_id
      tongRongPaymentList(this.query).then(response => {
        if(response.code==200){
          this.tongrong_plan_list = response.data
        }
        this.listLoading2 = false
      })
    },
    handleChange(v){
      if(v.indexOf('2')!=-1){
        this.getHaierList();
      }
      if(v.indexOf('3')!=-1){
        this.getTongRongList();
      }
    },
    getList() {
      this.query.order_id = this.$route.params.order_id
      paymentList(this.query).then(response => {
        this.list = response.data.list
        this.order = response.data.order
        this.project = response.data.project
        this.listLoading = false
      })
    },
    getPayWay(){
      getPayWay().then(res => {
        this.payway = res.data;
      })
    },
    excelout() {
      this.query.order_id = this.$route.params.order_id;
      exceloutPlan(this.query).then(response => {
        this.listLoading = false
        const filepath = response.data.path
        window.location.href = filepath
      })
    },
    tableRowClassName({row, rowIndex}) {
      if (row.s == 1) {
        return 'success-row';
      } else if (row.s == 2) {
        return 'warning-row';
      }
      return '';
    },
    tableRowClassNameHaier({row, rowIndex}) {
      let str = '';
      switch(row.paidFlag){
        case '20':case '30':case '40':case '50':
          str = 'success-row';
          break
        case '00':
          if(row.overDueFlag==1){
            str = 'warning-row';
          }else{
            str = '';
          }
          
          break
      }

      return str;
    },
    onSubmit(row) {
      const pam = {}
      pam.money_rate = row.money_rate;
      pam.is_paid_bf = row.is_paid_bf;
      pam.late_fee = row.late_fee;
      pam.paid_money = row.paid_money;// 实际还款金额
      pam.settle_money = row.settle_money;// 结算金额
      pam.pay_id = row.pay_id;
      pam.payway = row.payway;
      if(row.settle_day!=null){
        pam.settle_date = row.settle_day;// 结算日期
      }else{
         pam.settle_date = this.value1;// 结算日期
      }
      //pam.settle_date = this.value1// 结算日期
      pam.paid_date = this.value2;// 实际付款日期
      pam.is_settle = row.is_settle;// 实际付款日期
      // console.log(this.value1);
      // console.log(pam);return;
      paymentEdit(pam).then(resp => {
        this.$notify({
          title: '提示',
          message: '修改成功',
          type: 'success',
          duration: 2000,
          onClose: this.getList()
        })
      })
    },
    toBond(row) {
      this.$confirm(
        '确定将该条还款记录设置为保证金?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        const pam = {}
        pam.pay_id = row.pay_id
        pam.order_id = row.order_id
        toBond(pam).then(resp => {
          this.$notify({
            title: '提示',
            message: '修改成功',
            type: 'success',
            duration: 2000,
            onClose: this.getList()
          })
        })
      })
    }

  }
}
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    white-space: nowrap;
  }
  select{
    width:100px;
  }
  .el-table .warning-row {
      background: orangered;
    }

  .el-table .success-row {
    background: #98dd72;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .tb-edit .input-box {
    display: none
  }
  .tb-edit .current-cell .input-box {
    display: block;
    margin-left: -15px;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
    float: left;
  }
  .el-collapse-item__header {
    font-size: 20px;
    border-bottom: none;
    color: #3FA5E8
  }
</style>
